﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Skywave.IO
{
    public class PathHelper
    {
        public enum CopyMode
        {
            NoOverwrite,
            Overwrite,
            OverwriteIfNewer
        }

        public static bool Copy(System.IO.FileInfo source, System.IO.FileInfo target, CopyMode mode)
        {
            bool r = false;
            switch (mode)
            {
                case CopyMode.Overwrite:
                    source.CopyTo(target.FullName, true);
                    r = true;
                    break;
                case CopyMode.OverwriteIfNewer:
                    if (!target.Exists || source.LastWriteTime > target.LastWriteTime)
                    {
                        source.CopyTo(target.FullName, true);
                        r = true;
                    }
                    break;
                case CopyMode.NoOverwrite:
                default:
                    source.CopyTo(target.FullName, false);
                    break;
            }
            return r;
        }

        public static void Copy(string sourceDirectory, string targetDirectory, bool sourceShouldBeCreated, CopyMode mode)
        {
            System.IO.DirectoryInfo diSource = new System.IO.DirectoryInfo(sourceDirectory);
            System.IO.DirectoryInfo diTarget = new System.IO.DirectoryInfo(targetDirectory);

            CopyAll(diSource, diTarget, sourceShouldBeCreated, mode);
        }

        public static void CopyAll(System.IO.DirectoryInfo source, System.IO.DirectoryInfo target, bool sourceShouldBeCreated, CopyMode mode)
        {
            if (sourceShouldBeCreated)
            {
                target = new System.IO.DirectoryInfo(target.FullName + @"\" + source.Name);
            }
            // Check if the target directory exists, if not, create it.
            if (System.IO.Directory.Exists(target.FullName) == false)
            {
                System.IO.Directory.CreateDirectory(target.FullName);
            }

            // Copy each file into it's new directory.
            System.IO.FileInfo feDest1;
            foreach (System.IO.FileInfo fi in source.GetFiles())
            {
                //Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name);
                feDest1 = new System.IO.FileInfo(System.IO.Path.Combine(target.ToString(), fi.Name));
                Copy(fi, feDest1, mode);
            }

            // Copy each subdirectory using recursion.
            foreach (System.IO.DirectoryInfo diSourceSubDir in source.GetDirectories())
            {
                System.IO.DirectoryInfo nextTargetSubDir =
                    target.CreateSubdirectory(diSourceSubDir.Name);
                CopyAll(diSourceSubDir, nextTargetSubDir, false, mode);
            }
        }

        public static string ChangePath(string path, string fileNamePrefix, string fileNamePostfix)
        {
            return System.IO.Path.GetDirectoryName(path) + "\\" + fileNamePrefix + System.IO.Path.GetFileNameWithoutExtension(path) + fileNamePostfix + System.IO.Path.GetExtension(path);
        }
    }
}
